package sg.edu.nus.iss.cats.dataaccess.hibernate;

import java.util.List;

import sg.edu.nus.iss.cats.dataaccess.CourseDAO;
import sg.edu.nus.iss.cats.dataaccess.DAOException;
import sg.edu.nus.iss.cats.exception.HibernateAccessLayerException;
import sg.edu.nus.iss.cats.model.Course;
import sg.edu.nus.iss.cats.model.CourseEvent;

public class CourseDAOImpl extends AbstractDao implements CourseDAO {
	private static final String[] pendingStatuses = { Course.SUBMITTED,
			Course.UPDATED };

	public CourseDAOImpl() {
		super();
	}

	public Course findCourse(String courseId)
			throws HibernateAccessLayerException {

		/***********************************************************************
		 * WORKSHOP W10_hibernate 
		 * Complete the findCourse method - 
		 * 1. Convert the parameter courseId into an Integer object
		 * 2. call the super class AbstractDOA's find method by passing Class name and id as parameters
		 * 3. handle exception by throwing new HibernateAccessLayerException accordingly
		 **********************************************************************/
		System.out.println("Finding course" + courseId+pendingStatuses);
		Integer cid = new Integer(courseId);
		try {
			return ((Course) super.find(Course.class, cid));
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}
	}

	public Course[] findCoursesByEmployee(String employeeId)
			throws HibernateAccessLayerException {

		try {
			List list = super.findByCategory(Course.class,
					HibernateConstants.c_employeeId, employeeId);
			Course[] result = new Course[list.size()];
			list.toArray(result);
			return result;

		} catch (Throwable t) {

			throw new HibernateAccessLayerException(t.toString());

		}

	}

	public Course[] findCoursesPendingByEmployee(String employeeId)
			throws DAOException {
		System.out.println("find Pending courses of employee :" + employeeId);
		try {
			
			String sql = "select distinct {c.*} from course c, courseevent ce where c.courseid = ce.courseid and (ce.eventtype in (\'"+Course.SUBMITTED+"\' , \'"+
						Course.UPDATED+"\' ) ) and (c.status not in (\'"+Course.APPROVED+"\' , \'"+Course.REJECTED+"\' ) ) and c.employeeid = \'"+employeeId.trim()+"\'";
			List list = super.findByQuery(sql);
			System.out.println("List is " + list.toString());
			Course[] result = new Course[list.size()];
			for (int i =0; i<list.size(); i++)
			{
				result[i] = (Course) list.get(i);
			}
			return result;
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}

	public void insertCourse(Course course)
			throws HibernateAccessLayerException {
		/***********************************************************************
		 * WORKSHOP W10_hibernate 
		 * Complete the findCourse method - 
		 * 1. call the super class AbstractDOA's saveOrUpdate method by passing cousre as parameter
		 * 2. handle exception by throwing new HibernateAccessLayerException accordingly
		 **********************************************************************/

		System.out.println("Inserting course" + course.getCourseId());
		try {
			super.saveOrUpdate(course);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}

	}

	public void updateCourse(Course course)
			throws HibernateAccessLayerException {

		/***********************************************************************
		 * WORKSHOP W10_hibernate 
		 * Complete the findCourse method - 
		 * 1. call the super class AbstractDOA's saveOrUpdate method by passing cousre as parameter
		 * 2. handle exception by throwing new HibernateAccessLayerException accordingly
		 **********************************************************************/


		System.out.println("Updating course" + course.getCourseId());
		try {
			super.saveOrUpdate(course);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}

	}

	public void deleteCourse(Course course)
			throws HibernateAccessLayerException {
		System.out.println("Deleting course" + course.getCourseId());
		try {
			super.delete(course);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}
	}

	public void insertCourseEvent(CourseEvent event)
			throws HibernateAccessLayerException {

			System.out.println("Inserting event" + event.getCourseId());
		try {
			super.saveOrUpdate(event);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}
	}

	public void updateCourseEvent(CourseEvent event)
			throws HibernateAccessLayerException {
		System.out.println("Updating event" + event.getCourseId());
		try {
			super.saveOrUpdate(event);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}
	}

	public void deleteCourseEvent(CourseEvent event)
			throws HibernateAccessLayerException {
		System.out.println("Deleting event" + event.getCourseId());
		try {
			super.delete(event);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}
	}

}
